<?php
/**
 * 文章/微博 Redis
 * @App	
 * @Group 
 * @Action ArticlePublicRedis
 * @author lilei
 * @date 2018年8月10日
 *
 */
namespace app\common\redis;

use app\common\library\CommonFun;
use app\common\model\article\PublishArticleCoinModel;
use app\common\model\article\PublishArticleModel;
use app\common\model\article\PublishWeiboModel;
use app\common\model\CommonModel;

class ArticlePublicRedis extends RedisModel {
	/**
	 * Redis配置名称
	 * @var string
	 */
	public $redis = 'default';
	
	/**
	 * Redis key
	 * @var string
	 */
	public $key = '';
	
	/**
	 * Redis key 前缀
	 */
	public $key_prefix = 'public_aw';
	
	/**
	 * 创建文章的相关文章和微博
	 * @param int $article_id 文章编号
	 */
	public function CreateArticleRelateAW( $article_id ) {
		$articleModel = new PublishArticleModel();
		$articleData = $articleModel->get_data_by_id($article_id, true, true, false);
		if ( !$articleData ) {
			return false;
		}
		// 全局文章数量限制
		$alimit = 10;
		$articleMap = array(
				'id' => array('neq', $article_id),
				'is_published' => 1,
				'status' => 1,
				'deleted' => -1,
				'is_tmp' => -1,
				'article_type' => array('neq', PublishArticleModel::CMS_TYPE_DATADAILY),
		);
		$article_id_list = [];
		$coins = $articleData['coin_list'];
		
		if ( $coins ) {
			$articleCoinModel = new PublishArticleCoinModel();
			$articleCoinMap = [
					"article_id" => array("neq", $article_id),
					"coin_id" => array("in", $coins),
			];
			$coins_article_id_list = $articleCoinModel->search_id_list( $articleCoinMap, 1, false, 'create_time', 100, 'article_id' );
			$coins_article_id_list = $coins_article_id_list ? array_unique($coins_article_id_list) : [];
			if ( $coins_article_id_list ) {
				$articleMap['id'] = [
						array('neq', $article_id),
						array('in', $coins_article_id_list),
				];
				// 文章的相关文章列表
				$article_id_list = $articleModel->search_id_list($articleMap, 1, false, 'create_time', $alimit);
			}
		}
		
		// 没有直接的相关文章 @todo 临时处理方案
		if ( !$article_id_list ) {
			// 取出非币种文章
			$coinArticleRedis = CoinArticleRedis::getInstance();
			$page = time()%10;
			$limit = 4;
			$start = ($page-1)*$limit;
			$allTypeList = $coinArticleRedis->GetNoCoinAllTypeArticle();
			if ( $allTypeList ) {
				$aw_list = array_slice($allTypeList, $start, $limit);
				if ( $aw_list ) {
					foreach ( $aw_list as $aw_id ) {
						list( $type, $id ) = explode("_", $aw_id);
						if ($type == 'article' && $article_id != $id) {
							$article_id_list[] = $id;
							if ( count($article_id_list)==3 ) {
								break;
							}
						}
					}
				}
			}
		}
		
		$articleDataList = $article_id_list ? $articleModel->muti_get_data_by_id_list($article_id_list, true, true, false) : array();
		
		// 总列表
		$list = [];
		if ( $articleDataList ) {
			foreach ( $articleDataList as $v ) {
				$unique_key = CommonFun::create_unique_id();
				$coin_id = 0;
				if ( $v['coin_list'] ) {
					list($coin_id) = $v['coin_list'];
				}
				$_tmp = array(
						"unique_key" => $unique_key,
						"type" => 'article',
						"id" => (int) $v['id'],
						"coin_id" => $coin_id,
						"label_name" => '',
						"label_icon" => '',
						"sort" => strtotime($v['create_time']),
				);
				$list[] = $_tmp;
			}
		}
		
		if ( $list ) {
			CommonFun::array_sort($list, "sort");
			
			$key = "ArticleRelateAW:{$article_id}";
			$this->PipeStart();
			$this->SetRedisKey($key);
			if ( $this->ExistsKey() ) {
				$this->Delete();
			}
			foreach ( $list as $v ) {
				$this->ListRpush(CommonFun::json_encode2($v));
			}
			$this->PipeExec();
		}
		return true;
		
	}
	
	/**
	 * 创建微博的相关文章和微博
	 * @param int $weibo_id 微博文章编号
	 */
	public function CreateWeiboRelateAW( $weibo_id ) {
		$weiboModel = new PublishWeiboModel();
		$weiboData = $weiboModel->get_data_by_id($weibo_id);
		if ( !$weiboData ) {
			return false;
		}
		// 全局文章数量限制
		$alimit = 5;
		$weiboMap = array(
				'id' => array('neq', $weibo_id),
				'status' => 3, // 已通过
				'publish_status' => 2, //已发布
		);
		$_string = [];
		$weibo_id_list = [];
		$tags = $weiboData['tags'];
		$coins = $weiboData['coin_list'];
		
		// 没有相关依据属性
// 		if ( !$tags && !$coins ) {
// 			return false;
// 		}
		if ( $tags ) {
			foreach ( $tags as $tag_id ) {
				$_string[] = CommonModel::search_json_array($tag_id, 'tags');
			}
			
		}
		if ( $coins ) {
			foreach ( $coins as $coin_id ) {
				$_string[] = CommonModel::search_json_array($coin_id, 'coin_list');
			}
		}
		if ( $_string ) {
			$weiboMap['_string'] = implode(' or ', $_string);
			// 微博相关微博列表
			$weibo_id_list = $weiboModel->search_id_list($weiboMap, 1, false, 'create_time', $alimit);
		}
		
		// 没有直接的相关文章 @todo 临时处理方案
		if ( !$weibo_id_list ) {
			// 取出非币种文章
			$coinArticleRedis = CoinArticleRedis::getInstance();
			$page = time()%10;
			$limit = 4;
			$start = ($page-1)*$limit;
			$allTypeList = $coinArticleRedis->GetNoCoinAllTypeArticle();
			if ( $allTypeList ) {
				$aw_list = array_slice($allTypeList, $start, $limit);
				if ( $aw_list ) {
					foreach ( $aw_list as $aw_id ) {
						list( $type, $id ) = explode("_", $aw_id);
						if ($type == 'weibo' && $weibo_id != $id) {
							$weibo_id_list[] = $id;
							if ( count($weibo_id_list)==3 ) {
								break;
							}
						}
					}
				}
			}
		}
		
		$weiboDataList =  $weibo_id_list ? $weiboModel->muti_get_data_by_id_list($weibo_id_list) : array();
		
		$articleMap = array(
				'is_published' => 1,
				'status' => 1,
				'deleted' => -1,
				'is_tmp' => -1,
		);
		if ( $_string ) {
			$articleMap['_string'] = implode(' or ', $_string);
		}
		// 微博的相关文章列表
		$articleModel = new PublishArticleModel();
		$article_id_list = $articleModel->search_id_list($articleMap, 1, false, 'create_time', $alimit);
		$articleDataList = $article_id_list ? $articleModel->muti_get_data_by_id_list($article_id_list, true,true, false) : array();
		// 总列表
		$list = [];
		if ( $articleDataList ) {
			foreach ( $articleDataList as $v ) {
				$unique_key = CommonFun::create_unique_id();
				$coin_id = 0;
				if ( $v['coin_list'] ) {
					list($coin_id) = $v['coin_list'];
				}
				$_tmp = array(
						"unique_key" => $unique_key,
						"type" => 'article',
						"id" => (int) $v['id'],
						"coin_id" => $coin_id,
						"label_name" => '',
						"label_icon" => '',
						"sort" => strtotime($v['create_time']),
				);
				$list[] = $_tmp;
			}
		}
		if ( $weiboDataList ) {
			foreach ( $weiboDataList as $v ) {
				$unique_key = CommonFun::create_unique_id();
				$coin_id = 0;
				if ( $v['coin_list'] ) {
					list($coin_id) = $v['coin_list'];
				}
				$_tmp = array(
						"unique_key" => $unique_key,
						"type" => 'weibo',
						"id" => (int) $v['id'],
						"coin_id" => $coin_id,
						"label_name" => '',
						"label_icon" => '',
						"sort" => strtotime($v['create_time']),
				);
				$list[] = $_tmp;
			}
		}
		
		if ( $list ) {
			CommonFun::array_sort($list, "sort");
			
			$key = "WeiboRelateAW:{$weibo_id}";
			$this->PipeStart();
			$this->SetRedisKey($key);
			if ( $this->ExistsKey() ) {
				$this->Delete();
			}
			foreach ( $list as $v ) {
				$this->ListRpush(CommonFun::json_encode2($v));
			}
			$this->PipeExec();
		}
        return true;
	}

	/**
	 * 获取文章的相关文章和微博
	 * @param int $article_id 文章编号
	 * @param int $start 开始位置
	 * @param int $end 结束位置
	 * @return array
	 */
	public function GetArticleRelateAW ( $article_id, $start=0, $end=-1 ) {
		$key = "ArticleRelateAW:{$article_id}";
		$this->SetRedisKey($key);
		$res = $this->ListlRange($start, $end);
		if ( $res ) {
			foreach ( $res as &$v ) {
				$v = json_decode($v, true);
			}
		}
		return $res;
	}
	
	/**
	 * 计算文章的相关文章和微博数量
	 * @param int $article_id 文章编号
	 * @return int
	 */
	public function CountArticleRelateAW ( $article_id ) {
		$key = "ArticleRelateAW:{$article_id}";
		$this->SetRedisKey($key);
		$count = $this->ListSize();
		return  $count?:0;
	}
	
	/**
	 * 获取微博的相关文章和微博
	 * @param int $weibo_id 微博编号
	 * @param int $start 开始位置
	 * @param int $end 结束位置
	 * @return array
	 */
	public function GetWeiboRelateAW ( $weibo_id, $start=0, $end=-1 ) {
		$key = "WeiboRelateAW:{$weibo_id}";
		$this->SetRedisKey($key);
		$res = $this->ListlRange($start, $end);
		if ( $res ) {
			foreach ( $res as &$v ) {
				$v = json_decode($v, true);
			}
		}
		return $res;
	}
	
	/**
	 * 计算微博相关文章和微博数量
	 * @param int $weibo_id 微博编号
	 * @return int
	 */
	public function CountWeiboRelateAW ( $weibo_id ) {
		$key = "WeiboRelateAW:{$weibo_id}";
		$this->SetRedisKey($key);
		$count = $this->ListSize();
		return  $count?:0;
	}
}